<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<link rel="stylesheet" href="${RootUrl}/resources/jstree/themes/default/style.min.css" />
<script src="${RootUrl}/resources/jstree/jstree.min.js"></script>
	
<script>
// 格式化数据
function formatData(outputData){
	if($.isArray(outputData)){
		var initDatas = [];
		for(var i in outputData){
			var data = outputData[i]
			initDatas[i] = {
					id: data.id,
					text: data.name,
					children: true,
					state: {opened : false, selected : false}
				};
		}
		return initDatas;
	}else{
		return {
			id: outputData.id,
			text: outputData.name,
			children: true,
			state: {opened : false, selected : false}
		};
	}
}
// 树组件
$(".com_find").attr("bindAfterAction", "@fun:fun_find_success()")
var run = false;
function fun_find_success(){
	if(run){
		return;
	}
	run = true;
	var $this = $(this);
	// 初始数据
	var rootNode = {
			// text: $("#jstree_demo").attr("title"), 
			text: '',
			children: formatData($this.outputData().records), 
			state: {opened: true, disabled: false}
		};
	// 树
	var jstreeObj = $('#jstree_demo')
		.jstree({
			"core" : {
				"animation" : 200,
				"check_callback" : true,
				'force_text' : true,
				'themes' : {
					'responsive' : false,
					'variant' : 'small',
					'stripes' : true
				},
				'data' : function (obj, callback) {
					if(obj.id==='#'){
						callback.call(this, rootNode);
					}else{
						callback.call(this, []);
					}
		        },
		        'check_callback' : function(o, n, p, i, m) {return true;}
			},
			'contextmenu' : {
				'items' : function(node){
					var items = {
							"add" : {
								"separator_after"	: false,
								"label"				: "添加",
								"action"			: function (data) {
									var inst = $.jstree.reference(data.reference);
									var node = inst.get_node(data.reference);
									var $save = $(".com_a_save");
									if(node.parent === "#"){
										$save.attr("accessPara", "{parentId}<=")
									}else{
										$save.attr("accessPara", "{parentId}<="+node.id)
									}
									window.location.href=$save.access().get();
								}
							}
						};
					if(node.parent !== "#"){
						items["edit"]= {
								"separator_after"	: false,
								"label"				: "编辑",
								"action"			: function (data) {
									var inst = $.jstree.reference(data.reference);
									var node = inst.get_node(data.reference);
									var $edit = $(".com_a_edit");
									if(node.parent === "#"){
										$edit.attr("accessPara", "{id}<=")
									}else{
										$edit.attr("accessPara", "{id}<="+node.id)
									}
									window.location.href=$edit.access().get();
								}
							};
						items["delete"]= {
								"separator_after"	: false,
								"label"				: "删除",
								"action"			: function (data) {
									var inst = $.jstree.reference(data.reference);
									var node = inst.get_node(data.reference);
									$(".com_delete")
										.attr("accessPara", "{id}<="+node.id)
										.fireBind("bind", function(){
											inst.delete_node(node);
										});
								}
							};
					}
					return items;
				}
			},
			'plugins' : ['types','contextmenu']
		})
		.on("load_node.jstree", function(e, data){
			// 防止加载时执行
			if(!data.node.parent){
				return;
			}
			// 激活查找
			var $children = $(".com_find")
				.attr("accessPara", "{parentId}<="+data.node.id)
				.outputData(null)
				.fireBind("bind", null, function(){
					// 根据查找数据建立节点
					if(data.node.parent){
						var reference = $.jstree.reference(data.node);
						var outputData = $children.outputData().records;
						for(var index in outputData){
							reference.create_node(data.node, formatData(outputData[index]), "last");
						}
						// 打开节点
						reference.open_node(data.node)
					}
				});
		})
		.on("select_node.jstree", function(e, data){
			// 详情
			var $deatil = $(".iframe_detail");
			var rawSrc = $deatil.attr("rawSrc");
			if(data.node.parent === "#"){
				$deatil.removeAttr("src")
			}else{
				$deatil.attr("src", rawSrc.replace("\{id\}", data.node.id))
			}
			// 列表
			var $deatil = $(".iframe_page");
			if($deatil.length>0){
				var rawSrc = $deatil.attr("rawSrc");
				if(data.node.parent === "#"){
					$deatil.removeAttr("src")
				}else{
					$deatil.attr("src", rawSrc.replace("\{id\}", data.node.id))
				}
			}
		});
}
</script>